package com.movie.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.movie.model.auto.Role;
import com.movie.service.IRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 达瓦里氏
 * @since 2021-12-16
 */
@RestController
@RequestMapping("/role")
@CrossOrigin
public class RoleController {

    @Resource
    private IRoleService iRoleService;
    /*查询角色*/
    @GetMapping("/getRole")
    public List<Role> getRole(){
        return iRoleService.list();
    }

    /*通过id查询角色*/
    @GetMapping("/getRoleById/{id}")
    public Role getRoleById(@PathVariable("id") int id){
        return iRoleService.getById(id);
    }

    /*修改角色*/
    @PostMapping("/updateRole")
    public void updateRole(@RequestBody Role role){
        iRoleService.updateById(role);
    }

    /*添加角色*/
    @PostMapping("/setRole")
    public void setRole(@RequestBody Role role){
        iRoleService.save(role);
    }

    /*删除角色*/
    @PostMapping("/deletedById/{id}")
    public boolean deleted(@PathVariable("id") int id){
        return iRoleService.removeById(id);
    }


    /*批量删除角色*/
    @PostMapping("/deletedAllById")
    public boolean deletedAllById(@RequestBody int[] delarr){
        for (int i : delarr) {
            System.out.println(i);
            iRoleService.removeById(i);
        }
        return true;
    }

    /*模糊查询角色数据*/
    @PostMapping("/selectRole/{input}")
    public List<Map<String, Object>> selectInfo(@PathVariable("input") String value){
        System.out.println(value);
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        /*核心模糊算法*/
        queryWrapper.like("role",value).or().like("des",value);
        List<Map<String, Object>> maps = iRoleService.listMaps(queryWrapper);
        return maps;
    }


}
